package com.xpgk.mvc.statistic.customerForProductList;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bouncycastle.crypto.tls.MACAlgorithm;
import org.junit.Test;

import com.jfinal.aop.Before;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.xpgk.annotation.Service;
import com.xpgk.constant.ConstantRender;
import com.xpgk.dto.RenderBean;
import com.xpgk.mvc.base.BaseService;
import com.xpgk.mvc.bloseWeight.DelWeiTarget;
import com.xpgk.mvc.statistic.dailyNursing.DailyNursingStatisticSQL;
import com.xpgk.mvc.statistic.spropagandaTraining.SpropagandaTrainingSQL;
import com.xpgk.tools.ToolDateTime;


@Service(name = CustomerForProductListService.serviceName)
public class CustomerForProductListService extends BaseService{

	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(CustomerForProductListService.class);
	public static final String serviceName = "customerForProductListService";
	
	private String sql = CustomerForProductListSQL.customerforproductlist_splitPage;
	private String sqlProductType = CustomerForProductListSQL.customerforproductlist_queryproducttype;
	
	/*
	 * 顾客数据查询
	 */
	public List<Record> customerDataQuery(String organIdsEmployee,String arrivedMinDate) throws ParseException{
		Map<String, Object> queryParam = new HashMap<String, Object>();
	    queryParam.put("organIdsEmployee", organIdsEmployee);
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
	    Date date = sdf.parse(arrivedMinDate);
	    Map<String, Date> monthDate = ToolDateTime.getMonthDate(date);
	    queryParam.put("start",sdf.format(monthDate.get("start")));
	    queryParam.put("end",sdf.format(monthDate.get("end")));
	    List<Record> result = find(sql,queryParam);
	    for(Record r : result){
	    	String isuseproduct=null;
	    	if(r.get("count")!=null){
	    		isuseproduct="使用";
	    	}else{
	    		isuseproduct="未使用";
	    	}
	    	r.set("isuseproduct", isuseproduct);
	    	float sum=0;
	    	float jianzhong=0;
	    	float chanpin=0;
	    	float bili=0;
	    	queryParam.put("customerIds", r.getStr("ids"));
	    	List<Record> find = find(sqlProductType, queryParam);
	    	for(Record rr : find){
	    		Object chanplx = rr.get("chanplx");
	    		Object number1 = rr.get("number");
	    		Object price1 = rr.get("price");
	    		int number = Integer.parseInt(number1.toString());
	    		float price = Float.parseFloat(price1.toString());
	    		if(chanplx!=null && chanplx.toString().equals("455018290a8f4afbbaf29c61904d6406")){
	    			jianzhong = jianzhong+price;
	    		}else{
	    			chanpin = chanpin+price;
	    		}
	    		if(chanpin==0){
	    			bili= 99;
	    		}else{
	    			bili=jianzhong/chanpin;
	    		}
	    		r.set(rr.getStr("type")+"number", number);
	    		r.set(rr.getStr("type")+"price", price);
	    		sum = sum+price;
	    		r.set("sumprice", sum);
	    		r.set("bili", bili);
	    	}
	    }
		return result;
	}
}
